// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PySurfaceNormalAttributes.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>

// ****************************************************************************
// Module: PySurfaceNormalAttributes
//
// Purpose:
//   Attributes for SurfaceNormal operator
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a SurfaceNormalAttributes.
//
struct SurfaceNormalAttributesObject
{
    PyObject_HEAD
    SurfaceNormalAttributes *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewSurfaceNormalAttributes(int);
std::string
PySurfaceNormalAttributes_ToString(const SurfaceNormalAttributes *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    const char *centering_names = "Point, Cell";
    switch (atts->GetCentering())
    {
      case SurfaceNormalAttributes::Point:
          snprintf(tmpStr, 1000, "%scentering = %sPoint  # %s\n", prefix, prefix, centering_names);
          str += tmpStr;
          break;
      case SurfaceNormalAttributes::Cell:
          snprintf(tmpStr, 1000, "%scentering = %sCell  # %s\n", prefix, prefix, centering_names);
          str += tmpStr;
          break;
      default:
          break;
    }

    return str;
}

static PyObject *
SurfaceNormalAttributes_Notify(PyObject *self, PyObject *args)
{
    SurfaceNormalAttributesObject *obj = (SurfaceNormalAttributesObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SurfaceNormalAttributes_SetCentering(PyObject *self, PyObject *args)
{
    SurfaceNormalAttributesObject *obj = (SurfaceNormalAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    int cval = int(val);

    if ((val == -1 && PyErr_Occurred()) || long(cval) != val)
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ int");
    }

    if (cval < 0 || cval >= 2)
    {
        std::stringstream ss;
        ss << "An invalid centering value was given." << std::endl;
        ss << "Valid values are in the range [0,1]." << std::endl;
        ss << "You can also use the following symbolic names:";
        ss << " Point";
        ss << ", Cell";
        return PyErr_Format(PyExc_ValueError, ss.str().c_str());
    }

    Py_XDECREF(packaged_args);

    // Set the centering in the object.
    obj->data->SetCentering(SurfaceNormalAttributes::Centering(cval));

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SurfaceNormalAttributes_GetCentering(PyObject *self, PyObject *args)
{
    SurfaceNormalAttributesObject *obj = (SurfaceNormalAttributesObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetCentering()));
    return retval;
}



PyMethodDef PySurfaceNormalAttributes_methods[SURFACENORMALATTRIBUTES_NMETH] = {
    {"Notify", SurfaceNormalAttributes_Notify, METH_VARARGS},
    {"SetCentering", SurfaceNormalAttributes_SetCentering, METH_VARARGS},
    {"GetCentering", SurfaceNormalAttributes_GetCentering, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
SurfaceNormalAttributes_dealloc(PyObject *v)
{
   SurfaceNormalAttributesObject *obj = (SurfaceNormalAttributesObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *SurfaceNormalAttributes_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PySurfaceNormalAttributes_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "centering") == 0)
        return SurfaceNormalAttributes_GetCentering(self, NULL);
    if(strcmp(name, "Point") == 0)
        return PyInt_FromLong(long(SurfaceNormalAttributes::Point));
    if(strcmp(name, "Cell") == 0)
        return PyInt_FromLong(long(SurfaceNormalAttributes::Cell));



    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PySurfaceNormalAttributes_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PySurfaceNormalAttributes_methods[i].ml_name),
                PyString_FromString(PySurfaceNormalAttributes_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PySurfaceNormalAttributes_methods, self, name);
}

int
PySurfaceNormalAttributes_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "centering") == 0)
        obj = SurfaceNormalAttributes_SetCentering(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
SurfaceNormalAttributes_print(PyObject *v, FILE *fp, int flags)
{
    SurfaceNormalAttributesObject *obj = (SurfaceNormalAttributesObject *)v;
    fprintf(fp, "%s", PySurfaceNormalAttributes_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
SurfaceNormalAttributes_str(PyObject *v)
{
    SurfaceNormalAttributesObject *obj = (SurfaceNormalAttributesObject *)v;
    return PyString_FromString(PySurfaceNormalAttributes_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *SurfaceNormalAttributes_Purpose = "Attributes for SurfaceNormal operator";
#else
static char *SurfaceNormalAttributes_Purpose = "Attributes for SurfaceNormal operator";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(SurfaceNormalAttributesType,         \
                  "SurfaceNormalAttributes",           \
                  SurfaceNormalAttributesObject,       \
                  SurfaceNormalAttributes_dealloc,     \
                  SurfaceNormalAttributes_print,       \
                  PySurfaceNormalAttributes_getattr,   \
                  PySurfaceNormalAttributes_setattr,   \
                  SurfaceNormalAttributes_str,         \
                  SurfaceNormalAttributes_Purpose,     \
                  SurfaceNormalAttributes_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
SurfaceNormalAttributes_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &SurfaceNormalAttributesType
         || Py_TYPE(other) != &SurfaceNormalAttributesType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    SurfaceNormalAttributes *a = ((SurfaceNormalAttributesObject *)self)->data;
    SurfaceNormalAttributes *b = ((SurfaceNormalAttributesObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static SurfaceNormalAttributes *defaultAtts = 0;
static SurfaceNormalAttributes *currentAtts = 0;

static PyObject *
NewSurfaceNormalAttributes(int useCurrent)
{
    SurfaceNormalAttributesObject *newObject;
    newObject = PyObject_NEW(SurfaceNormalAttributesObject, &SurfaceNormalAttributesType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new SurfaceNormalAttributes(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new SurfaceNormalAttributes(*defaultAtts);
    else
        newObject->data = new SurfaceNormalAttributes;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapSurfaceNormalAttributes(const SurfaceNormalAttributes *attr)
{
    SurfaceNormalAttributesObject *newObject;
    newObject = PyObject_NEW(SurfaceNormalAttributesObject, &SurfaceNormalAttributesType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (SurfaceNormalAttributes *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
SurfaceNormalAttributes_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewSurfaceNormalAttributes(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef SurfaceNormalAttributesMethods[] = {
    {"SurfaceNormalAttributes", SurfaceNormalAttributes_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *SurfaceNormalAttributesObserver = 0;

std::string
PySurfaceNormalAttributes_GetLogString()
{
    std::string s("SurfaceNormalAtts = SurfaceNormalAttributes()\n");
    if(currentAtts != 0)
        s += PySurfaceNormalAttributes_ToString(currentAtts, "SurfaceNormalAtts.", true);
    return s;
}

static void
PySurfaceNormalAttributes_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("SurfaceNormalAtts = SurfaceNormalAttributes()\n");
        s += PySurfaceNormalAttributes_ToString(currentAtts, "SurfaceNormalAtts.", true);
        cb(s);
    }
}

void
PySurfaceNormalAttributes_StartUp(SurfaceNormalAttributes *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PySurfaceNormalAttributes_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(SurfaceNormalAttributesObserver == 0)
    {
        SurfaceNormalAttributesObserver = new ObserverToCallback(subj,
            PySurfaceNormalAttributes_CallLogRoutine, (void *)data);
    }

}

void
PySurfaceNormalAttributes_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete SurfaceNormalAttributesObserver;
    SurfaceNormalAttributesObserver = 0;
}

PyMethodDef *
PySurfaceNormalAttributes_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return SurfaceNormalAttributesMethods;
}

bool
PySurfaceNormalAttributes_Check(PyObject *obj)
{
    return (obj->ob_type == &SurfaceNormalAttributesType);
}

SurfaceNormalAttributes *
PySurfaceNormalAttributes_FromPyObject(PyObject *obj)
{
    SurfaceNormalAttributesObject *obj2 = (SurfaceNormalAttributesObject *)obj;
    return obj2->data;
}

PyObject *
PySurfaceNormalAttributes_New()
{
    return NewSurfaceNormalAttributes(0);
}

PyObject *
PySurfaceNormalAttributes_Wrap(const SurfaceNormalAttributes *attr)
{
    return WrapSurfaceNormalAttributes(attr);
}

void
PySurfaceNormalAttributes_SetParent(PyObject *obj, PyObject *parent)
{
    SurfaceNormalAttributesObject *obj2 = (SurfaceNormalAttributesObject *)obj;
    obj2->parent = parent;
}

void
PySurfaceNormalAttributes_SetDefaults(const SurfaceNormalAttributes *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new SurfaceNormalAttributes(*atts);
}

